package cn.znzz.cjm.service.impl;

import cn.znzz.cjm.domain.VipUser;
import cn.znzz.cjm.dto.VipCod;
import cn.znzz.cjm.feign.VipCodFeignClient;
import cn.znzz.cjm.mapper.VipUserMapper;
import cn.znzz.cjm.service.IVipUserService;
import cn.znzz.cjm.util.AjaxResult;
import cn.znzz.cjm.util.BitStatesUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.validation.Valid;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author chenjiangmin
 * @since 2021-12-25
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VipCodFeignClient feignClikent;
    @Autowired
    private VipUserMapper vipUserMapper;
    @Override
    public AjaxResult register(@Valid VipCod cod) {
        //redis拿到验证码是否存在
        Object reidscod = redisTemplate.opsForValue().get("zhuce::" + cod.getMobile());
        if(reidscod==null){
            throw new RuntimeException("验证码已过期，请重新获取");
        }
        //判断短信验证码是否正确
        String smsCode = cod.getSmsCode();
        if(!smsCode.equalsIgnoreCase(reidscod.toString())){
            throw new RuntimeException("手机验证码错误，请重新输入");
        }
        //存入数据t_login_user
        AjaxResult result=feignClikent.register(cod);
        if(result.isSuccess()){
            Integer id =(Integer) result.getResultObj();
            //存数据t_vip_user
            VipUser vipUser = new VipUser();
            vipUser.setPhone(cod.getMobile());
            vipUser.setLoginId(Long.valueOf(id));
            vipUser.setPassword(cod.getPassword());
            Long bitStatesUtils=BitStatesUtils.OP_ACTIVED;
            bitStatesUtils=BitStatesUtils.OP_AUTHED_PHONE;
            bitStatesUtils=BitStatesUtils.OP_INIT_PAY_PASSWORD;
            vipUser.setBitState(bitStatesUtils);
            vipUserMapper.insert(vipUser);
        }
        return result;
    }
}
